//
//  StatusRetweetedView.swift
//  sinaBlog
//
//  Created by tianxiangyang on 15/12/19.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

import UIKit
import SnapKit

class StatusRetweetedView: UIView {
    
    
    //底部约束属性
    
    var bottomConstrain:Constraint?
    
    //模型属性
    
    var retweentedStatus:BlogStatus?{
        
        didSet{
            
            if let retweeted = retweentedStatus {
                
                textLabel.text = "@" + (retweeted.user!.name ?? "") + (retweeted.text ?? "")
                
                pictureView.status = retweeted
                
                
                //卸载底部约束
                
                bottomConstrain?.uninstall()
                
                //根据模型数据动态设置底部约束
                if let pics = retweeted.pictureURLs where pics.count > 0 {
                    //有配图,修改约束.
                    
                    self.snp_updateConstraints(closure: { (make) -> Void in
                        self.bottomConstrain = make.bottom.equalTo(pictureView).offset(statusMargin).constraint
                    })
                    
                    
                }else{
                    //无配图,修改约束.
                    self.snp_updateConstraints(closure: { (make) -> Void in
                        self.bottomConstrain = make.bottom.equalTo(textLabel).offset(statusMargin).constraint
                    })
                    
                    
                }
                
                
            }
            
            
            
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
        
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:设置UI
    private func setupUI(){
        
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        addSubview(textLabel)
        addSubview(pictureView)
        
        //添加约束
        textLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self).offset(statusMargin)
            make.top.equalTo(self).offset(statusMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(textLabel.snp_bottom).offset(pictureViewMargin)
            make.left.equalTo(textLabel)
        }
        
        //自动计算高度
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstrain = make.bottom.equalTo(pictureView).offset(statusMargin).constraint
        }
        
    }
    
    //MARK:懒加载子控件
    
    private lazy var textLabel:UILabel = UILabel(title: "转发微博", text_Color: UIColor.darkGrayColor(), fontSize: 14, maxWidth: ScreenWidth - statusMargin * 2)
    
    private lazy var pictureView:StatusPictureView = StatusPictureView()
    
    
    
    
    
    
}
